package com.moana.buyTicket.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.ContextLoader;
import com.google.gson.Gson;


/**
 * 封装了一些Http常用方法
 * 
 * @author swan
 * 
 */
public class ServletUtil {

	public static final ServletContext context = ContextLoader.getCurrentWebApplicationContext().getServletContext();

	/**
	 * 设置页面无缓存，以及编码为utf-8
	 * 
	 * @param response
	 */
	public static final void expireData(HttpServletResponse response) {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", -10);
		response.setCharacterEncoding("utf-8");
	}
	/**
	 * 返回json内容
	 * @param response
	 * @param o
	 */
	public static final void sendJsonBean(HttpServletResponse response, Object o) {
		try {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", -10);
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().print(new Gson().toJson(o));
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
	}

	/**
	 * 返回html内容
	 * 
	 * @param response
	 * @param content
	 * @throws IOException
	 */
	public static final void sendContent(HttpServletResponse response, String content) {
		try {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", -10);
			response.setCharacterEncoding("utf-8");
			response.setContentType("html;charset=UTF-8");
			response.getWriter().print(content);
		} catch (IOException e) {
			e.printStackTrace();
		}
		;
	}


	/**
	 * 读取post过来的内容
	 * @param request
	 * @return
	 */
	public static String readStreamParameter(HttpServletRequest request) {		
		StringBuilder buffer = new StringBuilder();
		BufferedReader reader = null;
		try {
			ServletInputStream in=request.getInputStream();
			reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return buffer.toString();
	}
}
